package com.example.srm.organization.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.srm.auth.anno.Authority;
import com.example.srm.common.controller.BaseController;
import com.example.srm.constant.OrganizationConstant;
import com.example.srm.constant.ProjectConstant;
import com.example.srm.domain.TOrganization;
import com.example.srm.domain.TProject;
import com.example.srm.domain.vo.Member;
import com.example.srm.domain.vo.PageView;
import com.example.srm.domain.vo.ResearchAchievements;
import com.example.srm.domain.vo.Result;
import com.example.srm.organization.mapper.TOrganizationMapper;
import com.example.srm.organization.service.TOrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author pfk
 * @creatTime 2021/12/01 16:09
 * @describe    机构操作
 */
@RestController
@RequestMapping("/organization")
@Slf4j
@CrossOrigin
public class OrganizationController extends BaseController<TOrganizationService, TOrganization> {

    @Autowired
    private TOrganizationService organizationService;
    @Autowired
    private TOrganizationMapper organizationMapper;

    /**
     * 多条件分页查询
     */
    @RequestMapping(value = "/query1",method = RequestMethod.GET)
    @Authority(auth = {"account:organization:list"})
    public Result query(Integer page, Integer pageSize, String organizationName, String organizationType,String college, String achievementType){
        //校验参数是否合法
        if (page==null||page<0){
            page=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=10;
        }
        Page<TOrganization> tPage;
        QueryWrapper<TOrganization> queryWrapper = new QueryWrapper<TOrganization>();
        if (organizationName!=null&&organizationName!=""){
            queryWrapper.eq(OrganizationConstant.organizationName,organizationName);
            log.info("organizationName-->"+organizationName);
        }
        if (organizationType!=null&&organizationType!=""){
            queryWrapper.eq(OrganizationConstant.organizationType,organizationType);
            log.info("organizationType-->"+organizationType);
        }
        if (college!=null&&college!=""){
            queryWrapper.eq(OrganizationConstant.affiliatedCollage,college);
            log.info("affiliatedCollage-->"+college);
        }
        tPage = organizationMapper.selectPage(new Page<TOrganization>(page,pageSize),queryWrapper);
        List<TOrganization> organizationList = tPage.getRecords();
        //将json的members、researchAchievements转换为对象集合
        organizationList.forEach(e->{
            e.setMembers(JSONObject.parseArray(e.getMemberJson(), Member.class));
            e.setResearchAchievements(JSONObject.parseArray(e.getResearchAchievementsJson(), ResearchAchievements.class));
        });

        PageView<TOrganization> pageView = new PageView<TOrganization>()
                .setRows(organizationList)
                .setTotalCount(tPage.getTotal())
                .setTotalPage(new Integer(tPage.getPages()+""));
        return new Result().setData(pageView).setSuccess(true);
    }

    @Override
    @Authority(auth = {"account:organization:add"})
    public Result save(@RequestBody TOrganization tOrganization, HttpServletRequest request) throws Exception {
        return super.save(tOrganization, request);
    }

    @Override
    @Authority(auth = {"account:organization:update"})
    public Result update(@RequestBody TOrganization tOrganization, HttpServletRequest request) throws Exception {
        return super.update(tOrganization, request);
    }

    @Override
    @Authority(auth = {"account:organization:delete"})
    public Result delete(String id) {
        return super.delete(id);
    }

    @Override
    public Result findById(String id) {
        TOrganization t = baseIService.getById(id);
        t.setMembers(JSONObject.parseArray(t.getMemberJson(), Member.class));
        t.setResearchAchievements(JSONObject.parseArray(t.getResearchAchievementsJson(), ResearchAchievements.class));
        return new Result().setData(t).setSuccess(true);
    }

    /**
     * 保存前的操作
     * @param tOrganization
     * @throws Exception
     */
    @Override
    public void beforeSave(TOrganization tOrganization) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        tOrganization.setMemberJson(JSONObject.toJSONString(tOrganization.getMembers()))
        .setResearchAchievementsJson(JSONObject.toJSONString(tOrganization.getResearchAchievements()))
         .setEstablishmentTime(dateFormat.format(dateFormat.parse(tOrganization.getEstablishmentTime())));


    }

    /**
     * 更新前的操作
     * @param tOrganization
     * @throws Exception
     */
    @Override
    public void beforeUpdate(TOrganization tOrganization) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        tOrganization.setMemberJson(JSONObject.toJSONString(tOrganization.getMembers()))
        .setResearchAchievementsJson(JSONObject.toJSONString(tOrganization.getResearchAchievements()))
        .setEstablishmentTime(dateFormat.format(dateFormat.parse(tOrganization.getEstablishmentTime())));
    }
}
